home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / FILBUF.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  77 lines

  1. /* This is file FILBUF.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1980 Regents of the University of California.
  9.  * All rights reserved.  The Berkeley software License Agreement
  10.  * specifies the terms and conditions for redistribution.
  11.  */
  12.  
  13. #if defined(LIBC_SCCS) && !defined(lint)
  14. static char sccsid[] = "@(#)filbuf.c    5.3 (Berkeley) 3/9/86";
  15. #endif LIBC_SCCS and not lint
  16.  
  17. #include    <stdio.h>
  18. #include    <sys/types.h>
  19. #include    <sys/stat.h>
  20. char    *malloc();
  21.  
  22. _filbuf(iop)
  23. register FILE *iop;
  24. {
  25.     int size;
  26.     struct stat stbuf;
  27.     extern char *_smallbuf;
  28.     char c;
  29.  
  30.     if (iop->_flag & _IORW)
  31.         iop->_flag |= _IOREAD;
  32.  
  33.     if ((iop->_flag&_IOREAD) == 0)
  34.         return(EOF);
  35.     if (iop->_flag&(_IOSTRG|_IOEOF))
  36.         return(EOF);
  37. tryagain:
  38.     if (iop->_base==NULL) {
  39.         if (iop->_flag&_IONBF) {
  40.             iop->_base = _smallbuf ? &_smallbuf[fileno(iop)] : &c;
  41.             goto tryagain;
  42.         }
  43.         if (fstat(fileno(iop), &stbuf) < 0 || stbuf.st_blksize <= NULL)
  44.             size = BUFSIZ;
  45.         else
  46.             size = stbuf.st_blksize;
  47.         if ((iop->_base = malloc(size)) == NULL) {
  48.             iop->_flag |= _IONBF;
  49.             goto tryagain;
  50.         }
  51.         iop->_flag |= _IOMYBUF;
  52.         iop->_bufsiz = size;
  53.     }
  54.     if (iop == stdin) {
  55.         if (stdout->_flag&_IOLBF)
  56.             fflush(stdout);
  57.         if (stderr->_flag&_IOLBF)
  58.             fflush(stderr);
  59.     }
  60.     iop->_cnt = read(fileno(iop), iop->_base,
  61.         iop->_flag & _IONBF ? 1 : iop->_bufsiz);
  62.     iop->_ptr = iop->_base;
  63.     if (iop->_flag & _IONBF && iop->_base == &c)
  64.         iop->_base = NULL;
  65.     if (--iop->_cnt < 0) {
  66.         if (iop->_cnt == -1) {
  67.             iop->_flag |= _IOEOF;
  68.             if (iop->_flag & _IORW)
  69.                 iop->_flag &= ~_IOREAD;
  70.         } else
  71.             iop->_flag |= _IOERR;
  72.         iop->_cnt = 0;
  73.         return(EOF);
  74.     }
  75.     return(*iop->_ptr++&0377);
  76. }
  77.